import 'package:aitao_flutter/home/controllers/home_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
import '../../common_ui/back_nav.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../routes/routes.dart';
import '../../style/box_style.dart';
import '../../style/color_style.dart';
import '../../style/edgeInsets_style.dart';
import '../../style/text_style.dart';
import '../../utils/log_utils.dart';
import '../../utils/sp_utils.dart';

class CloseYoungModePage extends StatefulWidget {
  const CloseYoungModePage({super.key});

  @override
  State<CloseYoungModePage> createState() => _CloseYoungModePageState();
}

class _CloseYoungModePageState extends State<CloseYoungModePage> {
  TextEditingController editingController = TextEditingController();
  HomeController homeController = Get.put(HomeController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorStyle.whiteColor,
      appBar: BackNavigation(
        title: '关闭青少年模式',
        leftBtnCallBack: () {
          debugPrint('返回');
          homeController.getYoungMode();
          Get.back();
        },
      ),
      body: Container(
          alignment: Alignment.center,
          padding: EdgeInsetsStyle.customMargin(80, 0, 80, 100),
          child: ListView(
            children: [
              Box.vBox(60),
              Text(
                '请输入密码',
                style: Styles.normalText_18,
                textAlign: TextAlign.center,
              ),
              Box.vBox30,
              PinCodeTextField(
                autoFocus: true,
                controller: editingController,
                cursorColor: ColorStyle.themeColor,
                keyboardType: TextInputType.number,
                onCompleted: (txt) {
                  debugPrint('txt:$txt');
                  closeYoung();
                },
                appContext: context, //context
                length: 4, //输入的长度
                onChanged: (value) {
                  editingController.text = value;
                }, //变化回调
              )
            ],
          )),
    );
  }

  void closeYoung() async {
    var result = await RequestManager().post(API.closeAdolescent,
        params: {'password': editingController.text, 'is_open': 0});
    WJLogUtils.wjLog(result);
    if (result != null && result['code'] == 0) {
      debugPrint('关闭青少年模式');
      SpUtil.saveYoungModeStatus(false);
      Get.offAllNamed(Routes.tabPage);
    }
  }
}
